<template>
  <el-card style="margin: 10px;">
    <div slot="header" class="phyHeader">
      <span>{{ title }}</span>
      <div style="float: right;line-height: 30px;">
        <el-switch v-model="isHidden" active-text="隐藏" inactive-text="显示" style="margin-right: 10px;" @change="loadTable" />
        <el-button style="float: right;" size="small" type="success" icon="el-icon-plus" @click="add">新增子表单</el-button>
      </div>
    </div>
    <div :key="ver">
      <el-card v-for="item in tables" :key="item.Id" class="table">
        <div slot="header" class="header">
          <span class="title">
            <el-divider direction="vertical" />{{ item.Show }}({{ item.TableName }})
          </span>
          <div class="opBtn">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="edit(item)" />
            <el-button
              v-if="item.IsMainTable == false"
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="drop(item)"
            />
          </div>
        </div>
        <div class="oplist">
          <el-button type="primary" size="mini" icon="el-icon-edit" circle @click="editCol(item)" />
        </div>
        <el-empty v-if="item.Columns == null || item.Columns.length == 0" description="未设置" />
        <w-table
          v-else
          :data="rows"
          :columns="item.Columns"
          :is-paging="false"
          :is-show-no="true"
        >
          <template slot="control" slot-scope="e">
            <formControl :key="e.column.key" :value="e.row[e.column.key]" :control="colCache[e.column.key]" :source="e.row" />
          </template>
        </w-table>
      </el-card>
    </div>
    <editPhyQueryTable :id="id" :form-id="formId" :visible.sync="visible" @close="closeEdit" />
    <eiditPhyTable :form-id="formId" :show="show" :table-id="id" :visible.sync="editVisible" @close="closeEdit" />
  </el-card>
</template>

<script>
import * as formApi from '@/customForm/api/form'
import { Gets, Delete } from '@/customForm/api/phyQueryTable'
import eiditPhyTable from './components/eiditPhyTable.vue'
import editPhyQueryTable from './components/editPhyQueryTable.vue'
export default {
  components: {
    editPhyQueryTable,
    eiditPhyTable
  },
  data() {
    return {
      title: '编辑表结构',
      formId: null,
      formData: null,
      show: null,
      id: null,
      isHidden: false,
      editVisible: false,
      visible: false,
      tables: [],
      ver: 0,
      rows: [],
      columns: []
    }
  },
  computed: {
    comId() {
      return this.$store.getters.curComId
    }
  },
  mounted() {
    this.formId = this.$route.params.id
    this.reset()
    this.loadTable()
  },
  methods: {
    edit(item) {
      this.id = item.Id
      this.visible = true
    },
    editCol(item) {
      this.id = item.Id
      this.show = item.Show
      this.editVisible = true
    },
    closeEdit(isRefresh) {
      if (isRefresh) {
        this.loadTable()
      }
    },
    add() {
      this.id = null
      this.visible = true
    },
    drop(row) {
      const title = '确认删除子表 ' + row.Show + '?'
      const that = this
      this.$confirm(title, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.submitDrop(row.Id)
      })
    },
    async submitDrop(id) {
      await Delete(id)
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      this.loadTable()
    },
    async reset() {
      const res = await formApi.Get(this.formId)
      this.formData = res
      this.title = res.FormName + '结构编辑'
    },
    initColumns(column, col) {
      column.Children.forEach(a => {
        const t = {
          key: a.Name,
          title: a.Label,
          align: 'left'
        }
        if (a.IsGroup === false) {
          t.minWidth = a.Width == null ? '100px' : a.Width + 'px'
          t.title = a.Label + (a.IsHidden ? '(隐藏)' : '')
        }
        if (a.Children != null && a.Children.length > 0) {
          t.children = []
          this.initColumns(a, t)
        }
        col.children.push(t)
      })
    },
    async loadTable() {
      const list = await Gets(this.formId, this.isHidden)
      this.tables = list.map(c => {
        const t = {
          Id: c.Id,
          Show: c.Show,
          TableName: c.TableName,
          IsMainTable: c.IsMainTable,
          Columns: []
        }
        if (c.Columns && c.Columns.length > 0) {
          t.Columns = c.Columns.map(a => {
            const col = {
              key: a.Name,
              title: a.Label,
              align: 'center'
            }
            if (a.IsGroup === false) {
              col.minWidth = a.Width == null ? '100px' : a.Width + 'px'
              col.title = a.Label + (a.IsHidden ? '(隐藏)' : '')
            }
            if (a.Children != null && a.Children.length > 0) {
              col.children = []
              this.initColumns(a, col)
            }
            return col
          })
        }
        return t
      })
      this.ver = this.ver + 1
    }
  }
}
</script>

  <style scoped>
  .table {
    margin-bottom: 10px;
  }
  .phyHeader {
    line-height: 40px;
    height: 40px;
  }
  .table .header {
    text-align: left;
  }
  .table .header .opBtn {
    float: right;
  }
  .table .header .title {
    font-size: 1.1rem;
    display: inline-block;
  }
  .table .header .title .el-divider--vertical {
    margin-right: 5px !important;
    width: 4px;
    margin: 0;
    height: 1.2em;
    background-color: #0095ff;
  }
  .table .oplist {
    width: 100%;
    padding: 10px;
    text-align: right;
  }
  </style>
